
import {
  getAtaListAllApi,
  addFtmAtaApi,
  updateFtmAtaApi,
  deleteFtmApi,
  getFtmAtaChapterApi
} from '@/services/ataPage.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    ataListLoading: false,
    ataList: [],
    addAtaLoading: false,
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    ataTreeLoading: false,
    ataTreeData: []

  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取故障基础信息列表
    async getAtaListAll({ commit, state, dispatch }, params) {
      commit('setState', { key: 'ataListLoading', payload: true });
      const { pageNum, pageSize } = state.pagination;
      const paramsMerge = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getAtaListAllApi(paramsMerge);
      dispatch('setting/setAcarsTime', {}, { root: true });
      commit('setState', { key: 'ataList', payload: rep.data.list });
      commit('setState', { key: 'pagination', payload: { ...state.pagination, total: rep.data.total }});
      commit('setState', { key: 'ataListLoading', payload: false });
    },
    // 新增
    async addFtmAta({ commit }, params) {
      commit('setState', { key: 'addAtaLoading', payload: true });
      try {
        const rep = await addFtmAtaApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addAtaLoading', payload: false });
      }
    },
    // 左边树
    async getFtmAtaChapter({ commit }) {
      commit('setState', { key: 'ataTreeLoading', payload: true });
      const rep = await getFtmAtaChapterApi();
      commit('setState', { key: 'ataTreeData', payload: rep.data });
      commit('setState', { key: 'ataTreeLoading', payload: false });
    },
    // 修改
    async updateFtmAta({ commit }, params) {
      commit('setState', { key: 'addAtaLoading', payload: true });
      try {
        const rep = await updateFtmAtaApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addAtaLoading', payload: false });
      }
    },
    async deleteRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'addAtaLoading', payload: true });
      await deleteFtmApi(params);
      commit('setState', { key: 'addAtaLoading', payload: false });
    }
  }
};
